package org.example.backend.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.backend.bean.UserBase;
import org.example.backend.common.Result;
import org.example.backend.dto.LoginDTO;
import org.example.backend.service.MyUserDetailsService;
import org.example.backend.service.QQEMailService;
import org.example.backend.service.UserBaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 啊昌
 * @date 2025/7/19 11:06
 * AuthController 认证中心
 */
@Tag(name = "认证中心")
@RestController
public class AuthController {

    private Logger logger = LoggerFactory.getLogger("com.mjduan.sl4j.logger");

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private UserBaseService userBaseService;

    @Autowired
    private QQEMailService qqeMailService;


    /**
     * 用户登录接口
     * 该接口用于处理用户的登录请求，验证用户凭据并返回登录结果。
     *
     * @param loginDTO 登录信息，包含用户名和密码等
     * @return 返回一个包含登录结果信息的Result对象
     */
    @PostMapping("/auth")
    @Operation(summary = "用户登录")
    public Result<Map<String, Object>> login(@RequestBody LoginDTO loginDTO) {
//            // 创建未认证的Authentication
//            UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
//                    loginDTO.getUsername(), loginDTO.getPassword());
//            // 认证用户
//            Authentication authentication = authenticationManager.authenticate(authRequest); //会跳转到MyUserDetailsService的loadUserByUsername
//            // 将认证信息存储到上下文
//            SecurityContextHolder.getContext().setAuthentication(authentication);
//
//            // 获取用户详情
//            UserDetails userDetails = (UserDetails) authentication.getPrincipal();
//            logger.info("用户登录成功：" + userDetails.getUsername());
        Result<Map<String, Object>> result = userBaseService.login(loginDTO);
        return result;
    }

    /**
     * 根据QQ邮箱获取验证码
     * 通过提供的QQ邮箱地址，发送验证码并返回结果
     *
     * @param email QQ邮箱地址
     * @return 返回一个包含操作结果的Map对象，封装在Result类中
     */
    @GetMapping("/emailCode")
    @Operation(summary = "根据qq邮箱获取到验证码")
    public Result<Map<String, Object>>  getQQEmailCode(String email) {
        Result<Map<String, Object>> result = qqeMailService.sendQQEMail(email);
        return result;
    }
}
